<template>
  <div class="music-player-card">
    <section class="audio">
      <audio id="audio">
      </audio>
    </section>
    <img class="avatar" id="avatar" src="../assets/images/music-avatar.jpg" alt=""/>

    <div class="label-item">

      <div class="music-name" id="name"></div>
      <div class="music-singer" id="singer"></div>
    </div>


    <div class="btn-group">
      <div id="random"><img class="small" src="../assets/icons/random-player.png" alt=""></div>
      <div id="left"><img class="small" src="../assets/icons/left-player.png" alt=""></div>
      <div id="player-btn"><img id="playerB" class="middle" alt="" :src="playerBtnImg"></div>
      <div id="right"><img class="small" src="../assets/icons/right-player.png" alt=""></div>
      <div id="item"><img class="small" src="../assets/icons/outline-more.png" alt=""></div>
    </div>

  </div>
</template>

<script lang="ts" setup>
import {onMounted,  ref} from "vue";


// 歌曲信息数组
const songsList: Array<MusicItem> = [
  {
    id: 'xxx-01',
    title: '天空之城',
    author: '久石让',
    path: '../src/assets/music/天空之城.mp4',
    avatar: './image/1.jpg',
    time: 0
  },
  {
    id: 'xxx-02',
    title: 'Celebrity',
    author: 'IU',
    path: '../src/assets/music/Celebrity.mp3',
    avatar: './image/2.jpg',
    time: 0
  },
  {
    id: 'xxx-03',
    title: 'The Rain',
    author: '久石让',
    path: '../src/assets/music/The Rain.mp4',
    avatar: './image/3.jpg',
    time: 0
  }
];


type MusicItem = {
  id: string,
  title: string,
  author: string,
  path: string,
  avatar: string,
  time: number
}
const playerBtnImg = ref("../src/assets/icons/circle-stop.png")

onMounted(() => {

  const audio = document.querySelector('#audio') as HTMLAudioElement
  const left = document.querySelector('#left') as HTMLDivElement
  const right = document.querySelector('#right') as HTMLDivElement
  const item = document.querySelector('#item') as HTMLDivElement
  const playBtn = document.querySelector('#player-btn') as HTMLDivElement
  const title = document.querySelector('#name') as HTMLDivElement // 歌曲标题
  const author = document.querySelector('#singer') as HTMLDivElement// 歌曲作者


// 当前播放歌曲
  let curSongIndex = 0;
// 是否在播放
  let isPlay = false;


// 初始化
  function init() {
    render(songsList[curSongIndex]);
  }


// 播放 / 暂停 切换
  function togglePlay() {
    if (!isPlay) {
      // 暂停 图标切换
      songPlay();
    } else {
      // 播放 图标切换
      songPause();
    }
  }

// 播放
  function songPlay() {
    isPlay = true;
    playerBtnImg.value="../src/assets/icons/circle-player.png"
    audio.play();
  }

// 暂停
  function songPause() {
    isPlay = false;
    playerBtnImg.value="../src/assets/icons/circle-stop.png"
    audio.pause();
  }

// 上一首
  function preSong() {
    if (curSongIndex > 0) {
      curSongIndex--;
      render(songsList[curSongIndex]);
      setTimeout(songPlay,1000)
    }
    if (curSongIndex==0){
      curSongIndex=songsList.length-1
      render(songsList[curSongIndex])
      setTimeout(songPlay,1000)
    }
  }

// 下一首
  function nextSong() {
    if (curSongIndex < songsList.length - 1) {
      curSongIndex++;
      render(songsList[curSongIndex]);
      setTimeout(songPlay,1000)
    }
    if (curSongIndex==songsList.length){
      curSongIndex=0
      render(songsList[curSongIndex])
      setTimeout(songPlay,1000)
    }
  }

  left.addEventListener("click", () => {
    preSong()
  })
  right.addEventListener("click", () => {
    nextSong()
  })
  playBtn.addEventListener("click", () => {
    togglePlay()
  })


// 内容渲染到页面
  function render(song: MusicItem) {
    title.innerHTML = song.title;
    author.innerHTML = song.author;
    audio.volume = 1; // 音量 0 ~ 1
    audio.src = song.path; // 音乐资源路径
  }


  init();
})


</script>

<style lang="less" scoped>
.music-player-card {
  height: 100%;
  width: 90%;
  box-shadow: rgba(50, 50, 93, 0.25) 0 50px 100px -20px,
  rgba(0, 0, 0, 0.3) 0px 30px 60px -30px, rgba(10, 37, 64, 0.35) 0px -2px 6px 0px inset;
  border-radius: 20px;
  backdrop-filter: blur(3px);
  background:url("@/assets/icons/transparent-big.png") rgba(0,0,0,0.2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-direction: column;

  .avatar {
    width: 50%;
    height: 50%;
    border-radius: 5px;
  }

  .label-item{

    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    .music-name {
      color: white;
;
    }

    .music-singer {
      color: #b1c7dc;
      font-size: 12px;
      margin-left: 12px
    }
  }



  .progress {
    width: 100%;
    margin: 0 10px;
    height: 20px;
    background-color: #92fe9d;
  }

  .btn-group {
    width: 100%;
    margin: 0 10px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
  }
}

.small {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.middle {
  width: 36px;
  height: 36px;
  cursor: pointer;
}

</style>